package com.ruoyi.project.purchase.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.framework.web.domain.ResultBean;
import com.ruoyi.project.purchase.domain.pojo.DepotDetailBean;
import com.ruoyi.project.purchase.domain.pojo.DepotHeadItem;
import com.ruoyi.project.purchase.domain.pojo.ParamBean;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.purchase.domain.RyQuality;
import com.ruoyi.project.purchase.service.IRyQualityService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 采购质检Controller
 *
 * @author you
 * @date 2022-09-25
 */
@RestController
@RequestMapping("/purchase/ryQuality")
public class RyQualityController extends BaseController
{
    @Autowired
    private IRyQualityService ryQualityService;

    /**
     * 查询采购质检列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryQuality:list')")
    @GetMapping("/list")
    public TableDataInfo list(RyQuality ryQuality)
    {
        startPage();
        ryQuality.setType("其他");
        ryQuality.setSubType("采购质检");
        List<RyQuality> list = ryQualityService.selectRyQualityList(ryQuality);
        return getDataTable(list);
    }

    /**
     * 导出采购质检列表
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryQuality:export')")
    @Log(title = "采购质检", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RyQuality ryQuality)
    {
        List<RyQuality> list = ryQualityService.selectRyQualityList(ryQuality);
        ExcelUtil<RyQuality> util = new ExcelUtil<RyQuality>(RyQuality.class);
        util.exportExcel(response, list, "采购质检数据");
    }


    /**
     * 获取采购质检详细信息
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryQuality:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryQualityService.selectRyQualityById(id));
    }

    /**
     * 新增采购质检
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryQuality:add')")
    @Log(title = "采购质检", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RyQuality ryQuality)
    {
        return toAjax(ryQualityService.insertRyQuality(ryQuality));
    }


    /**
     * --采购质检
     * 新增采购质检信息
     * @return
     * @throws Exception
     */
    @RequestMapping("/addDepotHeadAndDetail")
    public AjaxResult addDepotHeadAndDetail(@RequestBody DepotHeadItem depotHeadItem)
    {
        System.out.println("linkHeadId---->" + depotHeadItem.getLinkHeadId());
        return ryQualityService.addDepotHeadAndDetail(depotHeadItem);
    }

    /**
     *  -- 采购质检
     * 根据主表修改单据子表信息
     */
    @RequestMapping("/editQualityDetail")
    public AjaxResult editQualityDetail(@RequestBody DepotHeadItem depotHeadItem){
        System.out.println("depotHeadItem---->" + depotHeadItem);
        return ryQualityService.editDepotDetail(depotHeadItem);
    }

    /**
     * -- 采购质检
     * 获取采购订单主表及子表详细信息
     */
    @GetMapping(value = "/selectRyDepotDetailById/{id}")
    public AjaxResult selectRyDepotDetailById(@PathVariable("id") Long id)
    {
        return AjaxResult.success(ryQualityService.selectRyDepotDetailById(id));
    }

    /**
     * 新增采购质检单时关联采购订单id查询子表信息
     */
    @GetMapping("/selectBypurchaseOrderId")
    public AjaxResult selectBypurchaseOrderId(ParamBean paramBean){

        return AjaxResult.success(ryQualityService.selectOrderItemByDepotId(paramBean.getLinkHeadId()));
    }




    /**
     * 修改采购质检
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryQuality:edit')")
    @Log(title = "采购质检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RyQuality ryQuality)
    {
        return toAjax(ryQualityService.updateRyQuality(ryQuality));
    }

    /**
     * 删除采购质检
     */
    @PreAuthorize("@ss.hasPermi('purchase:ryQuality:remove')")
    @Log(title = "采购质检", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(ryQualityService.deleteRyQualityByIds(ids));
    }
}
